<?php
/**
 * This class provides static methods used for the OR mapping.<br/>
 *
 * @package lib.famteam.phporm
 * @author polikeiji
 * @static
 *
 */
class DatabaseLibrary {

	/**
	 * Convert all properties of an object passed as a parameter.<br/>
	 * The original object represents a row of a result of a SQL query,
	 * and the conversion is performed for using the object in PHP program.<br/>
	 * Strings are converted to appropriate character encoding, 
	 * and properties represent data time are converted. <br/>
	 * 
	 * @param object $object
	 * @return object The object whose properties are converted 
	 */
	public static function convertField($object) {
		$propertyVars = get_object_vars($object);
		foreach ($propertyVars as $propertyName => $propertyValue) {
//			$object->$propertyName = mb_convert_encoding($propertyValue, mb_internal_encoding(), DatabaseConfiguration::DATABASE_ENCODING);
			if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $propertyValue)) {
				$year = substr($propertyValue, 0, 4);
				$month = substr($propertyValue, 5, 2);
				$day = substr($propertyValue, 8, 2);
				$hour = substr($propertyValue, 11, 2);
				$minuite = substr($propertyValue, 14, 2);
				$second = substr($propertyValue, 17, 2);
				$object->$propertyName = mktime($hour, $minuite, $second, $month, $day, $year);
			}
/*
			else if (preg_match('/^\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}$/', $propertyValue)) {
				$year = substr($propertyValue, 0, 4);
				$month = substr($propertyValue, 4, 2);
				$day = substr($propertyValue, 6, 2);
				$hour = substr($propertyValue, 8, 2);
				$minuite = substr($propertyValue, 10, 2);
				$second = substr($propertyValue, 12, 2);
				$object->$propertyName = mktime($hour, $minuite, $second, $month, $day, $year);
			}
*/
		}
		return $object;
	}
	
	/**
	 * Check whether properties the passed object suit column types of mapped database table.
	 * 
	 * @param object $targetObject
	 * @return bool
	 */
	public static function typeMatch($targetObject) {
		$className = get_class($targetObject);
		$thisVars = get_object_vars(new $className());
		$targetVars = get_object_vars($targetObject);
		foreach ($thisVars as $varsName) {
			if (FieldLibrary::typeCheck($varsName, $targetVars[$varsName])) {
				return false;
			}
		}
		return true;
	}
	
}
?>